<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Protocols  Reference</title>
    <link rel="stylesheet" type="text/css" href="css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="css/highlight.css" />
    <meta charset='utf-8'>
    <script src="js/jquery.min.js" defer></script>
    <script src="js/jazzy.js" defer></script>
    
    <script src="js/lunr.min.js" defer></script>
    <script src="js/typeahead.jquery.js" defer></script>
    <script src="js/jazzy.search.js" defer></script>
  </head>
  <body>
    <a name="//apple_ref/swift/Section/Protocols" class="dashAnchor"></a>
    <a title="Protocols  Reference"></a>
    <header>
      <div class="content-wrapper">
        <p><a href="index.html">SwiftCoroutine 2.1.9 Docs</a> (88% documented)</p>
        <p class="header-right">
          <form role="search" action="search.json">
            <input type="text" placeholder="Search documentation" data-typeahead>
          </form>
        </p>
      </div>
    </header>
    <div class="content-wrapper">
      <p id="breadcrumbs">
        <a href="index.html">SwiftCoroutine Reference</a>
        <img id="carat" src="img/carat.png" />
        Protocols  Reference
      </p>
    </div>
    <div class="content-wrapper">
      <nav class="sidebar">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a href="Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Classes/CoChannel.html">CoChannel</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/BufferType.html">– BufferType</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/Receiver.html">– Receiver</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoChannel/Sender.html">– Sender</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoFuture.html">CoFuture</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoPromise.html">CoPromise</a>
              </li>
              <li class="nav-group-task">
                <a href="Classes/CoScope.html">CoScope</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Enums.html">Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Enums/CoChannelError.html">CoChannelError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/CoFutureError.html">CoFutureError</a>
              </li>
              <li class="nav-group-task">
                <a href="Enums/CoroutineError.html">CoroutineError</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Extensions.html">Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Extensions/DispatchQueue.html">DispatchQueue</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/Publisher.html">Publisher</a>
              </li>
              <li class="nav-group-task">
                <a href="Extensions/URLSession.html">URLSession</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Protocols/CoCancellable.html">CoCancellable</a>
              </li>
              <li class="nav-group-task">
                <a href="Protocols/CoroutineScheduler.html">CoroutineScheduler</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a href="Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a href="Structs/Coroutine.html">Coroutine</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">
        <section>
          <section class="section">
            <h1>Protocols</h1>
            <p>The following protocols are available globally.</p>

          </section>
          <section class="section task-group-section">
            <div class="task-group">
              <ul>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine13CoCancellableP"></a>
                    <a name="//apple_ref/swift/Protocol/CoCancellable" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine13CoCancellableP">CoCancellable</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Undocumented</p>

                        <a href="Protocols/CoCancellable.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">CoCancellable</span> <span class="p">:</span> <span class="kt">AnyObject</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:14SwiftCoroutine0B9SchedulerP"></a>
                    <a name="//apple_ref/swift/Protocol/CoroutineScheduler" class="dashAnchor"></a>
                    <a class="token" href="#/s:14SwiftCoroutine0B9SchedulerP">CoroutineScheduler</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>A protocol that defines how to execute a task.</p>

<p>This protocol has extension methods that allow to launch coroutines on a current scheduler.
Inside the coroutine you can use such methods as <code><a href="Structs/Coroutine.html#/s:14SwiftCoroutine0B0V5awaityyyyycXEKFZ">Coroutine.await(_:)</a></code>, <code><a href="Classes/CoFuture.html#/s:14SwiftCoroutine8CoFutureC5awaitxyKF">CoFuture.await()</a></code>,
and <code><a href="Protocols/CoroutineScheduler.html#/s:14SwiftCoroutine0B9SchedulerPAAE5awaityqd__qd__yKXEKlF">CoroutineScheduler.await(_:)</a></code> to suspend the coroutine without blocking a thread
and resume it when the result is ready.</p>

<p>To launch a coroutine, use <code>CoroutineScheduler.startCoroutine(_:)</code>.</p>
<pre class="highlight swift"><code><span class="c1">//execute coroutine on the main thread</span>
<span class="kt">DispatchQueue</span><span class="o">.</span><span class="n">main</span><span class="o">.</span><span class="n">startCoroutine</span> <span class="p">{</span>

    <span class="c1">//extension that returns CoFuture&lt;(data: Data, response: URLResponse)&gt;</span>
    <span class="k">let</span> <span class="nv">dataFuture</span> <span class="o">=</span> <span class="kt">URLSession</span><span class="o">.</span><span class="n">shared</span><span class="o">.</span><span class="nf">dataTaskFuture</span><span class="p">(</span><span class="nv">for</span><span class="p">:</span> <span class="n">url</span><span class="p">)</span>

    <span class="c1">//await result that suspends coroutine and doesn't block the thread</span>
    <span class="k">let</span> <span class="nv">data</span> <span class="o">=</span> <span class="k">try</span> <span class="n">dataFuture</span><span class="o">.</span><span class="nf">await</span><span class="p">()</span><span class="o">.</span><span class="n">data</span>

<span class="p">}</span>
</code></pre>

<p>The framework includes the implementation of this protocol for <code>DispatchQueue</code>
and you can easily make the same for other schedulers as well.</p>
<pre class="highlight swift"><code><span class="kd">extension</span> <span class="kt">OperationQueue</span><span class="p">:</span> <span class="kt">CoroutineScheduler</span> <span class="p">{</span>

    <span class="kd">public</span> <span class="kd">func</span> <span class="nf">scheduleTask</span><span class="p">(</span><span class="n">_</span> <span class="nv">task</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span> <span class="p">{</span>
        <span class="nf">addOperation</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>
    <span class="p">}</span>

<span class="p">}</span>
</code></pre>

                        <a href="Protocols/CoroutineScheduler.html" class="slightly-smaller">See more</a>
                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">CoroutineScheduler</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </section>
        <section id="footer">
          <p>&copy; 2020 <a class="link" href="https://github.com/belozierov/SwiftCoroutine" target="_blank" rel="external">Alex Belozierov</a>. All rights reserved. (Last updated: 2020-07-11)</p>
          <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.5</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
        </section>
      </article>
    </div>
  </body>
</div>
</html>
